package com.leaf.spring.y202003.day09.redis;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import redis.clients.jedis.Jedis;

/**
 * 单个节点redis操作
 * 
 * @Title:
 * @Description:
 * @Author: leaf
 * @Since:2020年3月9日
 * @Version:1.1.0
 */
public class TestSingleRedis {

	private static Jedis jedis;

	@BeforeClass
	public static void setUp() {

		jedis = new Jedis("192.168.89.80", 7002);
		jedis.auth("7001");
		
	}

	@AfterClass
	public static void tearDown() {
		jedis.disconnect();
	}

	@Test
	public void testString() {

		jedis.set("nick_name", "leaf");
		System.out.println("nick_name：" + jedis.get("nick_name"));

		jedis.append("nick_name", " is my love");
		System.out.println("append:" + jedis.get("nick_name"));

		System.out.println("nick_name 长度：" + jedis.strlen("nick_name"));

		jedis.mset("age", "19", "sex", "m", "address", "shanghai");
		System.out.println(jedis.mget("nick_name", "age", "sex"));

		System.out.println(jedis.incr("age"));

		System.out.println("del：" + jedis.del("address"));

	}

	@Test
	public void testMap() {

		Map<String, String> userMap = new HashMap<>();

		userMap.put("name1", "leaf1");
		userMap.put("age", "22");
		userMap.put("mobile", "333");

		jedis.hmset("user", userMap);

		Iterator<String> hkeys = jedis.hkeys("user").iterator();
		while (hkeys.hasNext()) {
			String key = hkeys.next();
			System.out.println(key + ":" + jedis.hget("user", key));
		}

	}

	@Test
	public void testList() {

		jedis.lpush("lauage", "Chinese", "English", "France");

		jedis.rpush("lauage", "Japan");

		System.out.println(jedis.llen("lauage"));

		System.out.println(jedis.lrange("lauage", 0, 3));

	}

	@Test
	public void testSet() {

		jedis.sadd("users", "zhangsan", "lisi", "wangwu");

		jedis.sadd("users", "leaf");

		System.out.println(jedis.smembers("users"));
		System.out.println("users中是否有leaf:" + jedis.sismember("users", "leaf"));
		System.out.println(jedis.scard("users"));

	}

}
